<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\common\controller\Backend;
use think\response\View;
use app\common\service\OssService;
use hg\apidoc\annotation as Apidoc;
use think\facade\Db;

/**
 * 文件管理
 * @Apidoc\Group("base")
 * @package app\admin\controller
 */
class FileInfo extends Backend
{
    /**
     * 初始化参数
     * @author 贾二小
     * @since 2021/7/3
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    public function online_edit(): View
    {
        return view();
    }
    public function online_index(): View
    {
        return view();
    }

    /**
     * 上传文件
     * @author 贾二小
     * @since 2021/6/24
     */
    public function upload(){

        $file = request()->file('file');
        //上传文件名
        $fileOriginalName = $file->getOriginalName();
        //文件大类 bytes单位
        $fileSize = $file->getSize();
        //获取上传文件扩展名
        $fileExtension = $file->getOriginalExtension();
        $fileObjectName = $file->md5();
        //OSS上传文件
        $filePathName = 'storage/'.date('Y').'/'.date('m').'/'.date('d').'/'.$fileObjectName.'.'.$fileExtension;
        $OSSData = OssService::up($filePathName,$file->getPathname());
        if($OSSData['success']){
            $data = [];
            $data['file_location'] = 1;
            $data['file_bucket'] = get_config('aliyunBucket');
            $data['file_origin_name'] = $fileOriginalName;
            $data['file_suffix'] = $fileExtension;
            $data['file_size_kb'] = round($fileSize/1024);
            $data['file_size_info'] = format_file_size($fileSize);
            $data['file_object_name'] = $fileObjectName;
            $data['file_path'] = $filePathName;
            $data['create_user_id'] = 1;
            Db::name('file_info')->insert($data);
            $this->success();
        }else{
            $this->error($OSSData['errorMsg']);
        }

    }



    /**
     * 文件查看
     * @author 贾二小
     * @since 2021/6/24
     */
    public function preview(){

        $id = input('id');

        //todo

        $this->success();
    }

    /**
     * 文件列表
     * @author 贾二小
     * @since 2021/6/24
     */
    public function list(){

        $data = [];

        $this->success($data);
    }
}